package me.youline.dataServer.controller;

import javax.servlet.http.HttpServletResponse;

import me.youline.dataServer.ConfigConstants;
import me.youline.dataServer.entity.UserFeedback;
import me.youline.dataServer.entity.UserToken;
import me.youline.dataServer.service.FeedbackService;
import me.youline.dataServer.service.authentication.UserTokenService;
import me.youline.dataServer.service.support.MsgBuilder;
import me.youline.dataServer.utils.ResponseTypeOutputUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 反馈消息业务控制器
 * @author linhan
 *
 */
@RequestMapping("/feedback")
@Controller
public class FeedBackController {
	
	private static final Logger logger = LoggerFactory.getLogger(MsgPraiseController.class);
	
	@Autowired
	private UserTokenService userTokenService;
	
	@Autowired
	private FeedbackService feedbackService;
	
	@RequestMapping(method=RequestMethod.POST)
	public void createFeedback(@RequestParam String content,@RequestParam(value = ConfigConstants.REQ_PARAM_APP_TOKEN_NAME)String tokenStr,HttpServletResponse response){
		try {
			UserToken  t= userTokenService.getUserTokenByTokenStr(tokenStr);
			UserFeedback feed = new UserFeedback();
			feed.setContent(content);
			feed.setUserName(t.getUserName());
			feed.setCreator(t.getUserId());
			feedbackService.createFeedback(feed);
			response.setStatus(HttpServletResponse.SC_CREATED);
			ResponseTypeOutputUtils.renderJson(response, MsgBuilder.success().toJSONString());
			return ;
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
			response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			ResponseTypeOutputUtils.renderJson(response, MsgBuilder.error().errorCode("100001").errorMsg("反馈消息创建业务执行失败").toJSONString());
			return;
		}
	}
}
